package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IStatementItemService statementItemService;


    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        if (statement.getCustomerName()==null&&statement.getCustomerPhone()==null&&statement.getActualArrivalTime()==null&&statement.getLicensePlate()==null&&statement.getCarSeries()==null&&statement.getServiceType()==null){
            throw new BusinessException("参数异常");
        }
        statement.setCreateTime(new Date());
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        Statement temp = statementMapper.selectByPrimaryKey(statement.getId());
        if (temp == null) {
            throw new BusinessException("参数异常");
        }
        if(!Statement.STATUS_CONSUME.equals(temp.getStatus())){
            throw new BusinessException("非消费中状态不能修改");
        }
        statementMapper.updateStatement(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void updateAmount(Long id, BigDecimal totalAmount, BigDecimal totalCount, BigDecimal itemPrice) {
        statementMapper.updateAmount(id,totalAmount,totalCount,itemPrice);
    }

    @Override
    public void payStatement(Long statementId) {
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        statement.setPayTime(new Date());
        statement.setPayeeId(ShiroUtils.getUserId());
        statement.setStatus(Statement.STATUS_PAID);
        statementMapper.updatePay(statement);
        if (statement.getAppointmentId() != null) {
            appointmentService.changeStatus(statement.getAppointmentId(),Appointment.STATUS_SETTLE);
        }
    }

    @Override
    public Statement queryByAppointmentId(Long id) {
        return statementMapper.selectByAppointmentId(id);
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        Appointment appointment = appointmentService.get(appointmentId);

        Statement statement = new Statement();
        if (Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())) {
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setCreateTime(new Date());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setServiceType(appointment.getServiceType()+0L);
            statement.setAppointmentId(appointment.getId());
            statement.setInfo(appointment.getInfo());
            statement.setStatus(Statement.STATUS_CONSUME);
            statementMapper.insert(statement);
            appointmentService.changeStatus(appointmentId,Appointment.STATUS_SETTLE);
        }else{
            statement = statementMapper.selectByAppointmentId(appointmentId);
        }
        return statement;
    }

    @Override
    public void delete(Long id) {
        //判断是否是预约的
        Statement statement = statementMapper.selectByPrimaryKey(id);
        if(statement.getAppointmentId()!=null){
            //软删除
            appointmentService.changeStatus(statement.getAppointmentId(),Appointment.STATUS_CANCEL);
        }


        statementMapper.deleteByPrimaryKey(id);
        statementItemService.deleteByStatementId(id);


    }

}
